<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use App\Http\Requests\UserPostRequest;

class ArticleController extends Controller
{
   /**
     * 文章列表页
     *
     * @return data
     */
	
	public function getList(Request $request)
	{
		//判断用户是否搜索
		if($request->input('name')){
			//查询所有专栏信息
			$data = DB::table('article')
            ->leftJoin('topic', 'topic.id', '=', 'article.tid')
            ->leftJoin('user', 'user.id', '=', 'article.uid')
            ->select('article.*', 'topic.title as ttitle','user.name')
            ->where('user.name','like','%'.$request->input('name').'%')
            ->where('astatus','=','0')
            ->paginate(5);
		}else{
			//查询所有文章
			$data = DB::table('article')
            ->leftJoin('topic', 'topic.id', '=', 'article.tid')
            ->leftJoin('user', 'user.id', '=', 'article.uid')
            ->select('article.*', 'topic.title as ttitle','user.name')
            ->where('astatus','=','0')
            ->paginate(5);
		}
            
		//获取所有参数
		$all = $request->all();

        //分配模板
		return view('/admin.article.list',['data'=>$data,'all'=>$all]);
	}

	 /**
     * 查看文章内容
     *
     * @return data
     */
	public function getViewarticle(Request $request)
	{
		//获取文章id
		$id = $request->input('id');

		//查询数据 
		$data = DB::table('article')
			->leftJoin('user','user.id','=','article.uid')
			->select('article.*','user.name')
			->where('article.id',$id)
			->get();

		//分配模板
		return view('/admin.article.viewarticle',['data'=>$data]);
	}

	/**
     * 执行屏蔽操作
     *
     * @return data
     */
	public function getShield(Request $request)
	{
		//获取要屏蔽的文章的id
		$id = $request->input('id');

		//查询cstatus的值
		$data = DB::table('article')
				->where('id',$id)
				->first();
				
		//提取status
		$astatus = $data->astatus;
	
		//判断status是否为0,执行相应的操作
		if($astatus==0)
		{	
			//执行屏蔽操作
			$res = DB::table('article')->where('id',$id)->update(['astatus'=>1]);

			//判断是否屏蔽成功
			if($res)
	        {
	            return redirect('admin/article/list')->with('success','屏蔽成功');
	        }else{
	            return back()->withInput()->with('error','屏蔽失败');
	        }
		}else{
			//执行取消屏蔽操作
			$res = DB::table('article')->where('id',$id)->update(['astatus'=>0]);

			//判断是否取消屏蔽成功
			if($res)
	        {
	            return redirect('admin/article/list')->with('success','取消屏蔽成功');
	        }else{
	            return back()->withInput()->with('error','取消屏蔽失败');
	        }
		}
	}

	/**
     * 屏蔽文章列表
     *
     * @return data
     */
	public function getRecycle(Request $request)
	{
		//判断用户是否搜索
		if($request->input('name')){
			//查询所有专栏信息
			$data = DB::table('article')
            ->leftJoin('topic', 'topic.id', '=', 'article.tid')
            ->leftJoin('user', 'user.id', '=', 'article.uid')
            ->select('article.*', 'topic.title as ttitle','user.name')
            ->where('user.name','like','%'.$request->input('name').'%')
            ->where('astatus','=','1')
            ->paginate(5);
		}else{
			//查询所有文章
			$data = DB::table('article')
            ->leftJoin('topic', 'topic.id', '=', 'article.tid')
            ->leftJoin('user', 'user.id', '=', 'article.uid')
            ->select('article.*', 'topic.title as ttitle','user.name')
            ->where('astatus','=','1')
            ->paginate(5);
		}
            
		//获取所有参数
		$all = $request->all();

        //分配模板
		return view('/admin.article.recycle',['data'=>$data,'all'=>$all]);
	}
  
       
}
